package net.easyjava.admin.controller;

import net.easyjava.admin.constants.MenuConstants;
import net.easyjava.admin.constants.Webconstants;
import net.easyjava.base.bean.UserBean;
import net.easyjava.base.manager.UserManager;
import net.easyjava.framework.web.security.UserSecurityAdminCookie;
import net.easyjava.tools.model.ModelBean;
import net.easyjava.tools.model.ModelConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by wenxiaoning on 15/12/18.
 */
@Controller
public class IndexController extends BaseController {
    private  Logger logger = LoggerFactory.getLogger(this.getClass().getName());
    @Resource
    private UserManager userManager;
    @Resource
    private UserSecurityAdminCookie userSecurityAdminCookie;


    /**
     * 访问域名进入登陆页面
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/")
    @ResponseBody
    public ModelAndView index1(
            HttpServletRequest request, HttpServletResponse response) {
        String message = ModelConstants.MESSAGE_ERROR;
        try {
            response.sendRedirect("/index.html");
            return null;
        }catch (Exception e){
            logger.error(e.getMessage(),e.fillInStackTrace());
            return null;
        }

    }

    /**
     * 进入登陆页面
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/login.html")
    @ResponseBody
    public ModelAndView loginHtml(
            HttpServletRequest request, HttpServletResponse response) {
        String message = ModelConstants.MESSAGE_ERROR;
        try {
            return new ModelAndView("login.html");
        }catch (Exception e){
            logger.error(e.getMessage(),e.fillInStackTrace());
            return null;
        }

    }

    /**
     * 进行登陆操作
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/login.do")
    @ResponseBody
    public ModelAndView loginIndex(
            @RequestParam String userName,
            @RequestParam String password,
            HttpServletRequest request, HttpServletResponse response) {
        String message = ModelConstants.MESSAGE_ERROR;
        try {
            //查看是否有用户名密码
            if("".equals(userName) || "".equals(password)){
                message = "用户名或密码不能为空";
                return new ModelAndView("/login.html","message",message);
            }
            ModelBean modelBean = userManager.loginByName(request, response);
            if(modelBean.getStatus() == ModelConstants.STATUS_SUCCESS){
                response.sendRedirect("/index.html");
                return null;
            }else{
                return new ModelAndView("login.html","message",modelBean.getMessage());
            }
        }catch (Exception e){
            logger.error(e.getMessage(),e.fillInStackTrace());
            return null;
        }

    }



    /**
     * 进入主页
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/index.html")
    @ResponseBody
    public ModelAndView index(
            HttpServletRequest request, HttpServletResponse response) {
        String message = ModelConstants.MESSAGE_ERROR;
        try {
            String userId= this.getUserId();
            if(userId == null){
                //如果没有登陆跳转到登陆页面
                return new ModelAndView("/login.html","message","请先登录");
            }

            Map<String,Object> params = new HashMap<String,Object>();
            params.put("menu", MenuConstants.getList(0));
            System.out.println(MenuConstants.getList(0));
            return new ModelAndView("admin/index.html",params);
        }catch (Exception e){
            logger.error(e.getMessage(),e.fillInStackTrace());
            return null;
        }

    }

    /**
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/logout.do")
    @ResponseBody
    public ModelAndView logout(
            HttpServletRequest request, HttpServletResponse response) {
        String message = ModelConstants.MESSAGE_ERROR;
        try {
            userSecurityAdminCookie.removeUserSecurity(request,response);
            response.sendRedirect("/login.html");
            return null;
        }catch (Exception e){
            logger.error(e.getMessage(),e.fillInStackTrace());
            return null;
        }

    }






}
